package com.shidaimanong.demo.thread;

/**
 * 学编程，关注：十代码农
 * 抖音、快手、B站、西瓜视频、今日头条
 */
public class ThreadDemo5 {
    public static void main(String[] args) {
        Beauty beauty = new Beauty();
        //启动多个线程
        for (int i = 0; i < 10; i++) {
            createOneThread(beauty);
        }
    }

    /**
     * 启动一个线程
     */
    private static void createOneThread(Beauty beauty) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                beauty.increaseAge();
                System.out.println(beauty.getAge());
            }
        }).start();
    }

    static class Beauty {
        private int age = 0;

        /**
         * 年龄加1
         */
        public synchronized void increaseAge() {
            age++;
        }

        public int getAge() {
            return age;
        }
    }
}



